package main

import "fmt"

func adder() func(int) int {
	sum := 0
	return func(x int) int {
		sum += x
		return sum
	}
}

func doSomething() {

	defer fmt.Println("defer revoked ...")

	fmt.Println("do something ...")
}

func main() {
	pos, neg := adder(), adder() // 创建两个独立的闭包实例
	for i := 0; i < 3; i++ {
		fmt.Println(pos(i)) // 输出: 0, 1, 2, 3, ..., 9
	}

	for i := 0; i < 3; i++ {
		fmt.Println(neg(-2 * i)) // 输出: 0, -2, -6, -12, ..., -18
	}

	doSomething()
}
